package com.xuecheng.content.service.jobhandler;

import com.xuecheng.content.feignclient.CourseIndex;
import com.xuecheng.content.feignclient.SearchServiceClient;
import com.xuecheng.content.mapper.CoursePublishMapper;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MessageProcessAbstract;
import com.xuecheng.messagesdk.service.MqMessageService;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2022/9/22 10:16
 */
@Slf4j
@Component
public class CoursePublishTask extends MessageProcessAbstract {

    @Autowired
    CoursePublishService coursePublishService;
    
    @Autowired
    CoursePublishMapper coursePublishMapper;

    @Autowired
    SearchServiceClient searchServiceClient;



    @XxlJob("CoursePublishJobHandler")
    public void coursePublishJobHandler(){

        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        //调用抽象类的方法
        process(shardIndex,shardTotal,"course_publish",30,60);

    }





    //课程发布任务处理
    @Override
    public boolean execute(MqMessage mqMessage) {
        //获取消息相关的业务信息
        String businessKey1 = mqMessage.getBusinessKey1();
        long courseId = Integer.parseInt(businessKey1);
        //课程静态化
        generateCourseHtml(mqMessage,courseId);
        //课程索引
        saveCourseIndex(mqMessage,courseId);
//        //课程缓存
//        saveCourseCache(mqMessage,courseId);
        return true;
    }


    //生成课程静态化页面并上传至文件系统
    public void generateCourseHtml(MqMessage mqMessage,long courseId){

        log.debug("开始进行课程静态化,课程id:{}",courseId);
        //消息id
        Long id = mqMessage.getId();
        //消息处理的service
        MqMessageService mqMessageService = this.getMqMessageService();
        //消息幂等性处理
        int stageOne = mqMessageService.getStageOne(id);
        if(stageOne >0){
            log.debug("课程静态化已处理直接返回，课程id:{}",courseId);
            return ;
        }
//        try {
//            TimeUnit.SECONDS.sleep(10);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        //开始课程静态化，生成html页面
        File file = coursePublishService.generateCourseHtml(courseId);
        if(file == null){
            XxlJobHelper.handleFail("课程静态化页面生成失败");
            return ;
        }

        //将html页面保存到minio
        coursePublishService.uploadCourseHtml(courseId,file);


        //保存第一阶段状态
        mqMessageService.completedStageOne(id);
    }

//    //将课程信息缓存至redis
//    public void saveCourseCache(MqMessage mqMessage,long courseId){
//        log.debug("将课程信息缓存至redis,课程id:{}",courseId);
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }

    //保存课程索引信息
    public void saveCourseIndex(MqMessage mqMessage,long courseId){
        //任务ID
        Long id = mqMessage.getId();

        MqMessageService mqMessageService = this.getMqMessageService();

        //取出第二阶段状态
        int stageTwo = mqMessageService.getStageTwo(id);

        //任务幂等性处理
        if (stageTwo > 0){
            log.debug("课程索引已写入，课程id:{}",courseId);
            return ;
        }

        //查询课程信息，调用搜索服务添加索引接口
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        CourseIndex courseIndex = new CourseIndex();
        BeanUtils.copyProperties(coursePublish,courseIndex);
        //远程调用
        boolean add = searchServiceClient.add(courseIndex);
        if(!add){
            XxlJobHelper.handleFail("保存课程索引信息失败");
            return ;
        }
        //完成任务
        mqMessageService.completedStageTwo(id);
    }

}